<?php

/**
 * BannerPanelController is the default controller for this application
 */
class BannerPanelController extends BrowseControllerBase {
	/**
	 * BannerDataService Object
	 * @var BannerDataService
	 */
	private $bannerDataService;
	
	/**
	 * Constructor of index
	 */
	public function __construct() {
		parent::__construct ();
		
		/*
		//Check if User is logged in
		$currentUserId = $this->authentication->getCurrentUserId();
		if ($currentUserId == null) {
			// Go on to login page
			$url = Zend_Registry::get('config')->APPLICATION_URL . '/Browse/';
			CommonUtils::redirect($url);
			return -1;
		}
		*/
		
		$this->requireUserLogin ();
		
		// Init all DataService
		$this->bannerDataService = new BannerDataService ();
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing page of the site.
	 *
	 * @return void
	 *
	 */
	public function indexAction() {
		$authentication = new Authentication ();
		$arrElement = array ();
		$arrElement ['ownerId'] = $authentication->getCurrentUserId ();
		$arrElement ['status'] = BannerEntity::ACTIVE;
		
		$numRecordPerPage = 10;
		// Pager
		$start = $this->getQuery ( 'start', 0 );
		if ($start == '' || $start < 0) {
			$start = 0;
		}
		
		$totals = $this->bannerDataService->getTotals ( $arrElement );
		$itemList = $this->bannerDataService->getItems ( $arrElement, NULL, $start, $numRecordPerPage );
		
		if ($start > $totals) {
			$start = 0;
		}
		$link = Zend_Registry::get ( 'config' )->APPLICATION_URL . "/Browse/BannerPanel/Index";
		$pageLink = '';
		if ($totals > $numRecordPerPage) {
			$pageUtils = new PagerUtils ( $start, $totals, $numRecordPerPage, 10, $link );
			$pageLink = $pageUtils->writePagesLinks ( 0, 'start' );
			$pageLink .= "<br><span class=\"paing_text_nolink\">" . $pageUtils->writePagesCounter () . "</span>";
		}
		
		$this->smarty->assign ( 'pageLink', $pageLink );
		$this->smarty->assign ( 'itemList', $itemList );
		$this->smarty->assign ( 'itemCount', count ( $itemList ) );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/BannerPanel/list_banner.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
	
	public function createAction() {
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		// Get list category of Photo
		$authentication = new Authentication ();
		
		if (! empty ( $actionForm ) && $actionForm == 'submit' && $this->isDemoMode == false) {
			
			// get content
			$description = $this->getPost ( 'description', "" );
			
			// remove magic quotes
			if (get_magic_quotes_gpc ()) {
				$description = stripslashes ( $description );
			}
			
			$description = trim ( $description );
			
			// Main Title
			$title = $this->getPost ( 'title', "" );
			
			// Get URL of Banner
			$url = $this->getPost ( 'url', "" );
			
			$status = UserPhotoEntity::ACTIVE;
			
			// Upload picture
			$picture = '';
			try {
				$picture = CommonUtils::uploadPictureEx ( 'picture' );
			} catch ( Exception $ex ) {
				// Ignore this case
				$resultMessage = 'Upload banner fail.';
			}
			
			// check
			if (! empty ( $title )) {
				$status = intval ( $status );
				try {
					// Save data into database
					$bannerEntity = $this->bannerDataService->beforeCreate ();
					
					$bannerEntity->status = $status;
					$bannerEntity->picture = $picture;
					$bannerEntity->title = $title;
					$bannerEntity->description = $description;
					$bannerEntity->viewed = 0;
					$bannerEntity->url = $url;
					$bannerEntity->ownerId = $authentication->getCurrentUserId ();
					$this->bannerDataService->create ( $bannerEntity );
					
					// Redirect to edit page
					$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/BannerPanel/index';
					CommonUtils::redirect ( $url );
					exit ();
				} catch ( Exception $ex ) {
					$isError = true;
					// Ignore to process this case. Only show a message
					$error = 'The item cannot create successfully. Error Code = ' . $ex->getCode ();
				}
			} else {
				$isError = true;
				$error = 'Please fill in all required fields';
			}
		}
		
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/BannerPanel/create_photo.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
	
	public function editAction() {
		$authentication = new Authentication ();
		
		// Get id of page
		$id = $this->getPost ( 'id' );
		if (empty ( $id )) {
			$id = $this->getQuery ( 'id' );
		}
		// If the id of page does not exist, go on to view list page
		if (empty ( $id ) || ! is_numeric ( $id )) {
			$this->indexAction ();
			return false;
		}
		
		$id = intval ( $id );
		
		// Get data
		$bannerEntity = $this->bannerDataService->getById ( $id );
		
		// check data
		if (empty ( $bannerEntity )) {
			$this->indexAction ();
			return false;
		}
		
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		
		// submit
		if (! empty ( $actionForm ) && $actionForm == 'submit' && $this->isDemoMode == false) {
			// get content
			$description = $this->getPost ( 'description', "" );
			
			// remove magic quotes
			if (get_magic_quotes_gpc ()) {
				$description = stripslashes ( $description );
			}
			
			$description = trim ( $description );
			
			// Main Title
			$title = $this->getPost ( 'title', "" );
			$url = $this->getPost ( 'url', "" );
			
			$status = UserPhotoEntity::ACTIVE;
			
			// Upload picture
			$picture = '';
			try {
				$picture = CommonUtils::uploadPictureEx ( 'picture' );
			} catch ( Exception $ex ) {
				// Ignore this case
				$resultMessage = 'Upload banner fail.';
			}
			
			$bannerEntity->description = $description;
			if (! empty ( $picture )) {
				$bannerEntity->picture = $picture;
			}
			$bannerEntity->title = $title;
			$bannerEntity->url = $url;
			
			// check
			if (! empty ( $title )) {
				try {
					$res = $this->bannerDataService->update ( $bannerEntity );
				} catch ( Exception $ex ) {
					$error = 'The item cannot create successfully. Error Code = ' . $ex->getCode ();
					$isError = true;
				}
			} else {
				$isError = true;
				
				$error = 'Please fill in all required fields';
			}
		}
		
		$this->smarty->assign ( 'id', $bannerEntity->id );
		$this->smarty->assign ( 'description', $bannerEntity->description );
		$this->smarty->assign ( 'title', $bannerEntity->title );
		$this->smarty->assign ( 'url', $bannerEntity->url );
		$this->smarty->assign ( 'picture', $bannerEntity->picture );
		$this->smarty->assign ( 'bannerEntity', $bannerEntity );
		$userId = $authentication->getCurrentUserId ();
		
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/BannerPanel/edit_photo.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
	
	public function delAction() {
		// Get Id of page
		$id = $this->getQuery ( 'id', 0 );
		
		if (! empty ( $id ) && is_numeric ( $id ) && $this->isDemoMode == false) {
			
			$id = intval ( $id );
			// Delete page
			$res = $this->bannerDataService->del ( $id );
		}
		// Redirect to edit page
		$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/BannerPanel/index';
		CommonUtils::redirect ( $url );
		return;
	}
}
